#include "mainwindow.h"

#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QRandomGenerator>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent) {
    setupGameArea();
    gameTimer.setSingleShot(true);
    connect(&gameTimer, &QTimer::timeout, this, &MainWindow::endGame);

    moleTimer.setInterval(1000);
    connect(&moleTimer, &QTimer::timeout, this, &MainWindow::updateGame);
    hitMoleSound.setSource(QUrl::fromLocalFile(":/1.wav"));
    missMoleSound.setSource(QUrl::fromLocalFile(":/2.wav"));
    bombSound.setSource(QUrl::fromLocalFile(":/3.wav"));
    gameTimer.start(60000); // 游戏时间为 60 秒

    // 开局刷新2只地鼠和1个炸弹
    spawnMole();
    spawnMole();
    spawnBomb();

    moleTimer.start();

}
void MainWindow::spawnBomb() {
    if (moleLocations.size() >= holes.size()) {
        return;
    }

    int randomIndex;
    do {
        randomIndex = QRandomGenerator::global()->bounded(holes.size());
    } while (moleLocations.contains(randomIndex));

    QPushButton *hole = holes[randomIndex];
    hole->setIcon(QIcon(":/zha.png"));
    hole->setProperty("isBomb", true);
    moleLocations.insert(randomIndex);
}
void MainWindow::setupGameArea() {
    QVBoxLayout *mainLayout = new QVBoxLayout;
    scoreLabel = new QLabel(tr("分数：%1").arg(score));
    mainLayout->addWidget(scoreLabel);

    QGridLayout *gameGrid = new QGridLayout;

    for (int row = 0; row < totalRows; ++row) {
        for (int col = 0; col < totalColumns; ++col) {
            QPushButton *hole = new QPushButton;
            hole->setFixedSize(gridSize, gridSize);
            hole->setIconSize(QSize(gridSize, gridSize));
            connect(hole, &QPushButton::clicked, this, &MainWindow::hitMole);
            gameGrid->addWidget(hole, row, col);
            holes.append(hole);
        }
    }

    mainLayout->addLayout(gameGrid);

    QWidget *centralWidget = new QWidget;
    centralWidget->setLayout(mainLayout);
    setCentralWidget(centralWidget);
}

bool MainWindow::spawnMole() {
    if (moleLocations.size() >= holes.size()) {
        return false;
    }

    int randomIndex;
    do {
        randomIndex = QRandomGenerator::global()->bounded(holes.size());
    } while (moleLocations.contains(randomIndex));

    QPushButton *hole = holes[randomIndex];

    bool isBomb = QRandomGenerator::global()->bounded(100) < 10;
    if (isBomb) {
        QIcon bombIcon(":/zha.png");
        hole->setIcon(bombIcon);
        hole->setProperty("isBomb", true);
        hole->setProperty("hasMole", false); // 设置 "hasMole" 为 false
    } else {
        hole->setIcon(QIcon(":/shu.png"));
        hole->setProperty("hasMole", true);
        hole->setProperty("isBomb", false); // 设置 "isBomb" 为 false
    }

    moleLocations.insert(randomIndex);
    return true;
}


void MainWindow::hitMole() {
    QPushButton *hole = qobject_cast<QPushButton *>(sender());

    if (hole->property("isBomb").toBool()) {
        bombSound.play();
        endGame();
    } else if (hole->property("hasMole").toBool()) {
        ++score;
        scoreLabel->setText(tr("分数：%1").arg(score));
        hitMoleSound.play();
        hole->setIcon(QIcon());
        hole->setProperty("hasMole", false);
        int holeIndex = holes.indexOf(hole);
        moleLocations.remove(holeIndex);
        spawnMole();
    } else {
        missMoleSound.play();
    }
}

void MainWindow::updateGame() {
    QSet<int> oldMoleLocations = moleLocations;
    for (int index : oldMoleLocations) {
        QPushButton *hole = holes[index];
        hole->setIcon(QIcon());
        hole->setProperty("hasMole", false);
        hole->setProperty("isBomb", false);
        moleLocations.remove(index);
    }

    while (moleLocations.size() < 2) {
        spawnMole();
    }
    spawnBomb(); // 每秒刷新炸弹
}

void MainWindow::endGame() {
    moleTimer.stop();
    QMessageBox::information(this, tr("游戏结束"), tr("您的分数：%1").arg(score));
    QApplication::quit();
}
